package com.vcit.zhaosheng.student.controller;

import com.vcit.common.core.domain.AjaxResult;
import com.vcit.common.core.domain.model.StudentLoginUser;
import com.vcit.framework.web.service.TokenService;
import com.vcit.system.domain.SysNotice;
import com.vcit.system.domain.vo.ClazzResultVO;
import com.vcit.system.domain.vo.EnrollStudentVo;
import com.vcit.system.domain.vo.StudentListVo;
import com.vcit.system.service.ITClazzStudentService;
import com.vcit.system.service.enroll.EnrollService;
import com.vcit.system.service.student.ITStudentService;
import com.vcit.system.util.ReportCardWordUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Api("学生确认入学")
@RestController
@RequestMapping("/student/confirm")
public class StudentConfirmController {

    @Autowired
    private EnrollService enrollService;
    @Autowired
    private TokenService tokenService;

    @Autowired
    private ITClazzStudentService studentClazzService;

    @Autowired
    private ITStudentService studentService;

    /**
     * 获取学生招生详细信息
     */
    @ApiOperation("获取学生个人招生信息")
    @GetMapping("/info")
    public AjaxResult getInfo(HttpServletRequest request) {
       // StudentLoginUser loginUser =  tokenService.getStudentLoginUser(request);

        EnrollStudentVo student = enrollService.selectZhaoshengStudentById(getCurrentStudentId(request));
        return AjaxResult.success(student);
    }

    /**
     * 学生确认入学
     */
    @ApiOperation("学生确认入学")
    @PostMapping("/confirm")
    public AjaxResult confirm(HttpServletRequest request) {
      //  StudentLoginUser loginUser =  tokenService.getStudentLoginUser(request);
        EnrollStudentVo student = enrollService.selectZhaoshengStudentById(getCurrentStudentId(request));
        
        // 检查学生状态是否为已录取
        if (student == null || !Integer.valueOf(6).equals(student.getZsStatus())) {
            return AjaxResult.error("当前状态不允许确认入学");
        }
        
        // 更新学生状态为确认入学
        student.setZsStatus(7);
        enrollService.updateZhaoshengStudent(student);
        
        return AjaxResult.success("确认入学成功");
    }


    // 查询分班结果
    @GetMapping("/clazzResult")
    public AjaxResult getClazzResult(HttpServletRequest request) {
        Long studentId = getCurrentStudentId(request); // 实际项目用token获取
        ClazzResultVO result = studentClazzService.getClazzResult(studentId);
        return AjaxResult.success(result);

    }
    // 分班，录取 结果通知时间
    @GetMapping("/ResultNotice")
    @ApiOperation("获取学生通知结果")
    public AjaxResult getResultDate(HttpServletRequest request,String noticeType){
        Long studentId = getCurrentStudentId(request);
        StudentListVo student = studentService.selectTStudentById(studentId);
        SysNotice notice =  studentClazzService.getClazzResultNoticeDate(student,noticeType);
        return AjaxResult.success(notice);
    }

    // 下载报到卡
    @GetMapping("/reportCard/download")
    public void downloadReportCard(HttpServletResponse response,HttpServletRequest request) throws IOException {
        Long studentId = getCurrentStudentId(request);
        byte[] word =  studentClazzService.generateReportCard(studentId);
        response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        response.setHeader("Content-Disposition", "attachment; filename=reportCard.docx");
        response.getOutputStream().write(word);
    }


    private Long getCurrentStudentId(HttpServletRequest request) {
        // 这里根据你的登录实现获取学生ID
        StudentLoginUser loginUser =  tokenService.getStudentLoginUser(request);
        Long stuId=  loginUser.getStudent().getId();
        // 例如：return SecurityUtils.getCurrentUserId();
        return stuId;
    }
}
